package work.parabola.assets.controller.common.interceptor;

import java.util.Arrays;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
 * 
 * @author 黄松祥
 * 2018年4月2日 下午2:46:08
 */
public class LoginInterceptor implements HandlerInterceptor {
	
	private static final Logger log = LoggerFactory.getLogger(LoginInterceptor.class);

	@SuppressWarnings("rawtypes")
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		HandlerMethod handlerMethod = (HandlerMethod)handler;
		
		String methodName = handlerMethod.getMethod().getName();
        String className = handlerMethod.getBean().getClass().getSimpleName();
        
        StringBuffer requestParamBuffer = new StringBuffer();
        Map paramMap = request.getParameterMap();
        Iterator it = paramMap.entrySet().iterator();
        while (it.hasNext()){
            Map.Entry entry = (Map.Entry)it.next();
            String mapKey = (String)entry.getKey();

            String mapValue = StringUtils.EMPTY;

            //request这个参数的map，里面的value返回的是一个String[]
            Object obj = entry.getValue();
            if(obj instanceof String[]){
                String[] strs = (String[])obj;
                mapValue = Arrays.toString(strs);
            }
            requestParamBuffer.append(mapKey).append("=").append(mapValue);
        }
        if(className.equals("UserController")){
        	if(methodName.equals("login") || methodName.equals("logout")){
        		log.info("用户请求，className:{}, methodName:{}", className, methodName);
        		return true;
        	}
        	if(methodName.equals("updatePassword")){
        		log.info("用户请求，className:{}, methodName:{}", className, methodName);
        	}
        } else if(className.equals("NewsLawsController")){
        	//用户未登陆也可以查看新闻通知和法律法规列表
        	if(methodName.equals("getList")){
        		log.info("用户请求，className:{}, methodName:{}, 请求参数：{}", className, methodName, requestParamBuffer.toString());
        		return true;
        	}
        } else {
        	log.info("用户请求，className:{}, methodName:{}, 请求参数：{}", className, methodName, requestParamBuffer.toString());
        }
        
        
		/*
		 * UserBo userBo =
		 * (UserBo)request.getSession().getAttribute(Const.CURRENT_USER); if(userBo ==
		 * null){ response.reset();//这里要添加reset，否则报异常 getWriter() has already been
		 * called for this response.
		 * response.setCharacterEncoding("UTF-8");//这里要设置编码，否则会乱码
		 * if(className.equals("TemplateController") && methodName.equals("upload")){
		 * response.setContentType("text/plain;charset=UTF-8"); } else {
		 * response.setContentType("application/json;charset=UTF-8");//这里要设置返回值的类型，
		 * 因为全部是json接口。 } PrintWriter out = response.getWriter();
		 * out.print(JsonUtil.obj2String(ServerResponse.createByErrorCodeMessage(
		 * ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc())));
		 * out.flush(); out.close(); return false; }
		 */
		return true;
	}
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {

	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {

	}

}
